package handler

import (
	"github.com/gin-gonic/gin"
	"shopcar/api/request"
	"shopcar/internal/dao"
	"shopcar/internal/model"
)

func CommentAdd(c *gin.Context) {
	var req request.CommentAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": err.Error(),
		})
		return
	}
	var comment model.Comment
	dao.GetOneByFields(&model.Comment{ShopId: int(req.ShopId)}, &comment)
	if comment.Id != 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前评论已存在",
			"data": nil,
		})
		return
	}
	result := dao.Create(&model.Comment{
		UserId:  c.GetInt("UserId"),
		ShopId:  int(req.ShopId),
		Content: req.Content,
	})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "评论失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "评论成功",
		"data": nil,
	})
}

func CommentDel(c *gin.Context) {
	var req request.CommentRemove
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	var comment model.Comment
	dao.GetOneByFields(&model.Comment{Id: uint(req.Id)}, &comment)
	if comment.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前评论不存在",
			"data": nil,
		})
		return
	}
	result := dao.Delete(&model.Comment{Id: uint(req.Id)})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "删除评论失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除评论成功",
		"data": nil,
	})
}
